<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 挪车码
// +----------------------------------------------------------------------
namespace app\ymkj\controller;

use EasyWeChat\Foundation\Application;
use think\facade\Session;
use think\facade\Config;
use think\Controller;
use app\model\Nuoche as nuocheModel;
use think\Db;

class Nuoche extends Controller
{
  public function index()
  {
    $id = Request()->param('id');
    $res = Db::name('nuoche')->where('id',ltrim(Request()->param('id'),'0'))->find();
    if( $res ){
      if($res['type'] == 1){ // 激活状态跳转
        $sex = '';
        if($res['sex'] == 1){
          $sex = mb_substr($res['name'],0,1).'先生';
        } else {
          $sex = mb_substr($res['name'],0,1).'女士';
        }
        return redirect('nuoche/nuoche')
                  ->with(['name'=>$sex , 'carpai'=>$res['carpai'], 'tel'=>$res['tel'], 'nid' => $res['id']]);
      } else { // 未激活状态跳转
        return redirect('nuoche/jihuo')
                  ->with(['id'=>$id]);
      }
    } else {
      $this->error('卡号不存在');
    }
  }

  // 激活挪车码
  public function jihuo()
  {
    $options = Config::get('wewhat.wxConfig');
    $app = new Application($options);
    $oauth = $app->oauth;
    // 未登录
    if (empty(Session::get('wewhat_user'))) {
      Session::set('target_url','Nuoche/jihuo');
      return $oauth->redirect();
    }
    $id = Session('id');
    if( !$id ) return '<p style="font-size:50px;">id错误请重新扫描二维码</p>';
    $this->assign('id',$id);
    return view();
  }

  public function jihuoma()
  {
    if($this->request->isPost()){
      $user = Session::get('wewhat_user');
      $post = input('post.');
      unset($post['mianze']);
      $post['start_time'] = time();
      $post['end_time'] = time()+60*60*24*365;
      Db::startTrans();
      $res = Db::name('nuoche')->where('id','=',$post['id'])->update($post);
      if( $res ){
        $uid = Db::name('user')->where('openid','=',$user['openid'])->find();
        if( $uid ){
          Db::name('user')
            ->where('openid','=',$user['openid'])
            ->update(['nid'=>$post['id'],'vip' => 1]);
        } else {
          Db::name('user')
            ->insert(['nid'        => $post['id'],
                      'openid'     => $user['openid'],
                      'nickname'   => $user['nickname'],
                      'sex'        => $user['sex'],
                      'headimgurl' => $user['headimgurl'],
                      'name'       => $post['name'],
                      'tel'        => $post['tel'],
                      'start_time' => $post['start_time'],
                      'end_time'   => $post['end_time'],
                      'vip'        => 1,
                      ]);
        }
        Db::commit();
        $this->success('激活成功','user/index');
      } else {
        Db::rollback();
        $this->error('激活失败');
      }
    }
  }

  // 电话挪车
  public function nuoche(){
    $options = Config::get('wewhat.wxConfig');
    $app = new Application($options);
    $staff = $app->staff;
    $user = Session::get('wewhat_user');
    $staff->message("移车通知\n云萌科技汽车服务平台！")->to($user['openid'])->send();
    
    $nid = Session('nid');
    $model = new nuocheModel();
    $info = $model::with('user')->where('id','=',$nid)->find();
    // dump($info); die;
    // $info['name'] = Session('name');
    // $info['carpai'] = Session('carpai');
    // $info['tel'] = Session('tel');
    $this->assign('info',$info);
    return view();
  }

  // 免责声明
  public function mian(){
    return view();
  }

  
}
?>